/**
 * Copyright (c) 2009-2012 XTEAM
 * All rights reserved.
 */
/**
 * File：SysCfgDaoImpl.java
 * History:
 *         2012-6-12: Initially created, Chrise.
 */
package com.xteam.ecsystem.dao.Impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.xteam.ecsystem.dao.BaseDao;
import com.xteam.ecsystem.dao.ISysCfgDao;
import com.xteam.ecsystem.entities.SysSystemConfig;
import com.xteam.ecsystem.utils.PageCenterStation;

/**
 * 系统配置数据访问实现。
 * @author Chrise
 */
@Repository
public class SysCfgDaoImpl implements ISysCfgDao {
	
	@Autowired
	private BaseDao<SysSystemConfig> baseDao;
	
	/**
	 * @see com.xteam.ecsystem.dao.ISysCfgDao#getConfigsByNameAndState(int, int, int)
	 */
	public PageCenterStation<SysSystemConfig> getConfigsByNameAndState(int state, int first, int max) {
		String cdt = "";
		if (state != INVALID_STATE) {
			if ("".equals(cdt)) {
				cdt = " where sscState = " + state;
			} else {
				cdt += " and sscState = " + state;
			}
		}
		
		String countHql = "select count(sscId) from SysSystemConfig" + cdt;
		String queryHql = "from SysSystemConfig" + cdt;
		
		return this.baseDao.queryPage(countHql, queryHql, first, max);
	}
	
	/**
	 * @see com.xteam.ecsystem.dao.ISysCfgDao#updateState(java.lang.String, int)
	 */
	public void updateState(String id, int state) {
		String hql = "update SysSystemConfig set sscState = " + state + " where sscId = '" + id + "'";
		this.baseDao.executeUpdate(hql);
	}
	
	/**
	 * @see com.xteam.ecsystem.dao.ISysCfgDao#deleteConfigs(java.util.List)
	 */
	public void deleteConfigs(List<String> ids) {
		String cdt = "'" + ids.get(0) + "'";
		for (int index = 1; index < ids.size(); index ++) {
			cdt += ", '" + ids.get(index) + "'";
		}
		
		String hql = "delete from SysSystemConfig where sscId in (" + cdt + ")";
		this.baseDao.executeUpdate(hql);
	}
	
	/**
	 * @see com.xteam.ecsystem.dao.ISysCfgDao#isExist(java.lang.String)
	 */
	public boolean isExist(String name) {
		String hql = "select count(sscId) from SysSystemConfig where sscName = '" + name + "'";
		if (this.baseDao.count(hql) > 0) {
			return true;
		}
		
		return false;
	}
	
	/**
	 * @see com.xteam.ecsystem.dao.ISysCfgDao#isExist(java.lang.String, java.lang.String)
	 */
	public boolean isExist(String id, String name) {
		String hql = "select count(sscId) from SysSystemConfig where sscId <> '" + id + 
			"' and sscName = '" + name + "'";
		if (this.baseDao.count(hql) > 0) {
			return true;
		}
		
		return false;
	}
	
	/**
	 * @see com.xteam.ecsystem.dao.ISysCfgDao#insertConfig(com.xteam.ecsystem.entities.SysSystemConfig)
	 */
	public void insertConfig(SysSystemConfig ssc) {
		this.baseDao.save(ssc);
	}
	
	/**
	 * @see com.xteam.ecsystem.dao.ISysCfgDao#updateConfig(com.xteam.ecsystem.entities.SysSystemConfig)
	 */
	public void updateConfig(SysSystemConfig ssc) {
		this.baseDao.update(ssc);
	}
	
	/**
	 * @see com.xteam.ecsystem.dao.ISysCfgDao#getConfig(java.lang.String)
	 */
	public SysSystemConfig getConfig(String id) {
		String hql = "from SysSystemConfig where sscId = '" + id + "'";
		return this.baseDao.findUniqueObject(hql);
	}
	
	/**
	 * @see com.xteam.ecsystem.dao.ISysCfgDao#getConfigsByState(int)
	 */
	public List<SysSystemConfig> getConfigsByState(int state) {
		String hql = "from SysSystemConfig where sscState = " + state;
		return this.baseDao.queryList(hql);
	}
}
